{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "42156faa-00c5-4cc6-b8fd-7306578d3a9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import cv2\n",
    "image = cv2.imread('./img/image00623.jpeg')\n",
    "# print(gray.shape)\n",
    "adjusted = cv2.convertScaleAbs(image, alpha=1.5, beta=5)\n",
    "# 非局部均值去噪\n",
    "denoised_image = cv2.fastNlMeansDenoisingColored(image, None, 40, 10, 7, 21)\n",
    "# 将彩色图转为灰度图\n",
    "gray=cv2.cvtColor(denoised_image,cv2.COLOR_BGR2GRAY)\n",
    "cv2.imwrite('./img/image00623_gray.jpg',gray)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "ff76fc31-b4d5-4a3c-b64c-1275f8ee4583",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def wash_img(path,filename):\n",
    "    image = cv2.imread(path+'\\\\'+filename)\n",
    "    if image is None:\n",
    "        return False\n",
    "    adjusted = cv2.convertScaleAbs(image, alpha=1.5, beta=5)\n",
    "    # 非局部均值去噪\n",
    "    # 非局部均值去噪\n",
    "    # 参数说明：\n",
    "    # 25：去噪强度（值越大，去噪效果越强，但细节损失越多）。\n",
    "    # 10：模板窗口大小（用于计算像素相似性）。\n",
    "    # 7：搜索窗口大小（用于寻找相似像素）。\n",
    "    # 21：用于加权平均的像素块大小。\n",
    "    denoised_image = cv2.fastNlMeansDenoisingColored(adjusted, None, 25, 10, 7, 21)\n",
    "    # 将彩色图转为灰度图\n",
    "    gray=cv2.cvtColor(denoised_image,cv2.COLOR_BGR2GRAY)\n",
    "    cv2.imwrite(path+'\\\\'+'new_'+filename,gray)\n",
    "    return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "a5419664-c052-4b8e-9aa5-0386c062abc3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import cv2\n",
    "folder_path='.\\\\img'\n",
    "file_name = 'image00623.jpeg'\n",
    "wash_img(folder_path,file_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "d5966a03-9824-446a-a81e-d5faffd5115b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "./img/kongzi/1/\\image00869.jpeg\n",
      "True\n",
      "./img/kongzi/1/\\image00873.jpeg\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import cv2\n",
    "# 获取文件夹下的所有文件名\n",
    "folder_path='./img/kongzi/1/'\n",
    "file_names = os.listdir(folder_path)\n",
    "\n",
    "# 过滤掉文件夹，只保留文件\n",
    "file_names = [f for f in file_names if os.path.isfile(os.path.join(folder_path, f))]\n",
    "for file_name in file_names:\n",
    "    print(folder_path+'\\\\'+file_name)\n",
    "    ret = wash_img(folder_path,file_name)\n",
    "    print(ret)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
